package com.hanhai.material.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置
 *
 * @author youchuang
 * @since 2022-08-09
 */
@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {
    @Bean //配置docket以配置Swagger具体参数
    public Docket docket() {
        //RequestHandlerselectors,配置要扫描接口的方式
//        basePackage:指定要扫描的包
        //any():扫描全部
        //none():不扫描
        //withClassAnnotation:扫描类上的注解，多数是一个注解的反射对象
        //withMethodAnnotation:扫描方法上的注解
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
//         Authorization，可以自定义名称-->token
        tokenPar.name("Authorization").
                defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2ODYwMTkyMTcsInVzZXJuYW1lIjoieW91Y2h1YW5nIn0.0uiwdfT3nh1DwsGyaYJryGUEAFxklGvfjbvyvaFA628").description("令牌").modelRef(new ModelRef("string")).parameterType("Header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().
                apis(RequestHandlerSelectors.basePackage("com.hanhai.material")).build().globalOperationParameters(pars);
    }

    //配置文档信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("CC", "www.cctt1221.xyz", "youchuang@cctt1221.xyz");
        return new ApiInfoBuilder().title("瀚海").description("SpringBoot + Vue + Mybatis-Plus").version("v1.0").contact(contact).build();
    }

}
